package com.mall.shopping.services;

import com.mall.shopping.IProductService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.converter.ProductDetailConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.ItemDesc;
import com.mall.shopping.dal.persistence.ItemDescMapper;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dto.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;

@Service
public class IProductDetailServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    ProductDetailConverter productDetailConverter;
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse productDetailResponse = new ProductDetailResponse();
        ArrayList<String> stringArrayList = new ArrayList<>();
        try {
            request.requestCheck();
            Long id = request.getId();
            Item item = itemMapper.selectByPrimaryKey(id);
            ItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(id);
            String[] images = item.getImages();
            for (String image : images) {
                stringArrayList.add(image);
            }
            ProductDetailDto productDetailDto = productDetailConverter.productDOToDTO(item);
            productDetailDto.setProductImageSmall(stringArrayList);
            productDetailDto.setDetail(itemDesc.getItemDesc());
            productDetailResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            productDetailResponse.setProductDetailDto(productDetailDto);
        } catch (Exception e) {
            e.printStackTrace();
            productDetailResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            productDetailResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }
        return productDetailResponse;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        return null;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        return null;
    }
}
